﻿using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Ext.Net
{

    //public class CheckboxJsonConverter : ExtJsonConverter
    //{
    //    public override bool CanConvert(Type valueType)
    //    {
    //        return typeof(bool).IsAssignableFrom(valueType);
    //    }

    //    public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    //    {
    //        var value = reader.Value.ToString();
    //        bool b;
    //        return bool.TryParse(value, out b) ? b : !string.IsNullOrEmpty(value);
    //    }

    //    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    //    {
    //        writer.WriteValue(((bool)value).ToString());
    //    }
    //}


    public static class JsonUtils
    {
        public static Dictionary<string, object> ToDictionary(this JObject obj)
        {
            if (obj == null) return null;
            var d = new Dictionary<string, object>();
            foreach (var prop in obj.Properties())
            {
                d[prop.Name] = prop.Value is JObject || prop.Value is JArray
                    ? prop.Value : prop.Value.Value<object>();
            }
            return d;
        }
    }
}